package cn.phecda.boot.job.xxljob.handler;

import cn.phecda.boot.common.lang.web.RestClientApi;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * <p>http任务处理</p>
 *
 * @author wanpinwei
 */
@Slf4j
public class HttpJobHandler extends BaseJobHandler implements RestClientApi {

    @Override
    public String getBaseUrl() {
        return "";
    }

    @XxlJob("getHttpJobHanlder")
    public void getHttpJobHanlder() {
        Optional<String> url = getParam();
        if (url.isEmpty()) {
            return;
        }

        try {
            XxlJobHelper.log("Accessing web using the GET method, the url is " + url);
            String result = restClientBuilder().build().get().retrieve().body(String.class);
            XxlJobHelper.log("The web response result is " + result);

        } catch (Exception e) {
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail(e.getMessage());
        }

    }

    @XxlJob("postHttpJobHanlder")
    public void postHttpJobHanlder() {
        Optional<String> url = getParam();
        if (url.isEmpty()) {
            return;
        }

        try {
            XxlJobHelper.log("Accessing web using the POST method, the url:" + url);
            String result = restClientBuilder().build().post().retrieve().body(String.class);
            XxlJobHelper.log("The web response result is " + result);
        } catch (Exception e) {
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail(e.getMessage());
        }
    }

    @XxlJob("putHttpJobHanlder")
    public void putHttpJobHanlder() {
        Optional<String> url = getParam();
        if (url.isEmpty()) {
            return;
        }

        try {
            XxlJobHelper.log("Accessing web using the PUT method, the url:" + url);
            String result = restClientBuilder().build().put().retrieve().body(String.class);
            XxlJobHelper.log("The web response result is " + result);
        } catch (Exception e) {
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail(e.getMessage());
        }
    }

    @XxlJob("deleteHttpJobHanlder")
    public void deleteHttpJobHanlder() {
        Optional<String> url = getParam();
        if (url.isEmpty()) {
            return;
        }

        try {
            XxlJobHelper.log("Accessing web using the DELETE method, the url:" + url);
            String result = restClientBuilder().build().delete().retrieve().body(String.class);
            XxlJobHelper.log("The web response result is " + result);
        } catch (Exception e) {
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail(e.getMessage());
        }
    }


}
